package com.lolc;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.lolc.bean.Champion;
import com.lolc.bean.Lane;
import com.lolc.ihm.PlayerSelection;
import com.lolc.utils.Constant;
import com.lolc.utils.Utils;


public final class LoLAutoCounterManager {

	private static LoLAutoCounterManager instance;
	
	private PlayerSelection[] myTeamSelections;
	
	private Lane[] myTeamLanes;
	
	private Champion[] bannedChampions;
	
	private PlayerSelection[] ennemySelections;
	
	private Lane lane;
	
	private boolean isWithMeta;
	
	private HashMap<String,Champion> championsMap;
	
	private List<Champion> championsList;
			
	private List<Lane> lanesList;
	
	private HashMap<String, Lane> lanesMap;
	
	static final Logger logger = LogManager.getLogger(LoLAutoCounterManager.class.getName());
	
	public void setLanesList(List<Lane> lanesList) {
		this.lanesList = lanesList;
	}

	private LoLAutoCounterManager()
	{
		isWithMeta=true;
		this.myTeamSelections=new PlayerSelection[Constant.NUMBER_PLAYER_PER_TEAM];
		this.ennemySelections=new PlayerSelection[Constant.NUMBER_PLAYER_PER_TEAM];
		this.myTeamLanes=new Lane[Constant.NUMBER_PLAYER_PER_TEAM];
		this.bannedChampions=new Champion[Constant.NUMBER_BANNED_CHAMPIONS_PER_TEAM*2];
		
		this.loadChampions();
		this.loadLanes();
	}

	static public LoLAutoCounterManager getInstance() {
		if (null == instance) { // Premier appel
            instance = new LoLAutoCounterManager();
        }		
		return instance;
	}
	
	/**
	 * @return the isWithMeta
	 */
	public boolean isWithMeta() {
		return isWithMeta;
	}

	/**
	 * @param ennemySelections the ennemyChampions to set
	 */
	public void setEnnemySelections(PlayerSelection[] ennemySelection) {
		this.ennemySelections = ennemySelection;
	}

	/**
	 * @param isWithMeta the isWithMeta to set
	 */
	public void setWithMeta(boolean isWithMeta) {
		this.isWithMeta = isWithMeta;
	}
	
	public void loadChampions()
	{		
		this.championsMap = new HashMap<String, Champion>();
		this.championsList = Utils.loadChampionsXMLFile(Constant.CHAMPIONS_XML_FILE);
		for(final Champion champion : this.championsList)
		{
			this.championsMap.put(champion.getId(), champion);				
		}			
	}

	public void loadLanes()
	{		
		//List<String> championList = new ArrayList<String>();
		this.lanesList = Utils.loadLanesXMLFile(Constant.LANES_XML_FILE);
		this.lanesMap = new HashMap<String, Lane>();
		for(final Lane lane : this.lanesList)
		{
			this.lanesMap.put(lane.getId(), lane);				
		}
	}

	public void setBannedChampionAtPosition(final int position, final Champion bannedChampion)
	{
		this.bannedChampions[position]=bannedChampion;
	}
		
	public void setMyTeamSelectionAtPosition(final int position,final PlayerSelection selection)
	{				
		this.myTeamSelections[position]=selection;
	}
	
	public void setMyTeamChampionAtPosition(final int position, final Champion champion)
	{
		if(this.myTeamSelections[position]==null)
		{
			this.myTeamSelections[position]= new PlayerSelection();
		}
		this.myTeamSelections[position].setChampion(champion);
	}
	
	public void setEnnemyChampionAtPosition(final int position, final Champion champion)
	{
		if(this.ennemySelections[position]==null)
		{
			this.ennemySelections[position]=new PlayerSelection();
		}
		this.ennemySelections[position].setChampion(champion);
	}
	
	public void setEnnemyTeamSelectionAtPosition(final int position, final PlayerSelection selection)
	{				
		this.ennemySelections[position]=selection;
	}
	
	public void setEnnemyLaneAtPosition(final int position, final Lane lane)
	{
		if(this.ennemySelections[position]==null)
		{
			this.ennemySelections[position]=new PlayerSelection();
		}
		
		this.ennemySelections[position].setLane(lane);
		
	}
	
	public void setMyTeamLaneAtPosition(final int position, final Lane lane)
	{
		if(this.myTeamSelections[position]==null)
		{
			this.myTeamSelections[position]=new PlayerSelection();
		}
		
		this.myTeamSelections[position].setLane(lane);
	}
	
	public void setLaneAtPosition(final int position, final Lane lane)
	{
		this.myTeamLanes[position]=lane;
	}
		
	public List<Champion> getSelectedChampion()
	{
		final List<Champion> champions=new ArrayList<Champion>();
		for(int i=0;i< Constant.NUMBER_PLAYER_PER_TEAM;i++)
		{
			if(this.myTeamSelections[i]!=null && this.myTeamSelections[i].getChampion()!=null)
			{	    				
				champions.add(this.myTeamSelections[i].getChampion());					
			}

			if(this.ennemySelections[i]!=null && this.ennemySelections[i].getChampion()!=null)
			{	    				
				champions.add(this.ennemySelections[i].getChampion());					
			}		
		}

		for(int i=0;i< this.bannedChampions.length;i++)
		{
			if(this.bannedChampions[i]!=null)
			{
				champions.add(this.bannedChampions[i]);
			}
		}

		return champions;
	}
	
	/**
	 * @return the championsMap
	 */
	public HashMap<String, Champion> getChampionsMap() {
		return championsMap;
	}

	/**
	 * @return the championsList
	 */
	public List<Champion> getChampionsList() {
		return championsList;
	}

	/**
	 * @param championsMap the championsMap to set
	 */
	public void setChampionsMap(HashMap<String, Champion> championsMap) {
		this.championsMap = championsMap;
	}

	/**
	 * @param championsList the championsList to set
	 */
	public void setChampionsList(final List<Champion> championsList) {
		this.championsList = championsList;
	}

	/**
	 * @return the bannedChampions
	 */
	public Champion[] getBannedChampions() {
		return bannedChampions;
	}

	/**
	 * @param bannedChampions the bannedChampions to set
	 */
	public void setBannedChampions(Champion[] bannedChampions) {
		this.bannedChampions = bannedChampions;
	}
	/**
	 * @return the myTeamChampions
	 */
	public PlayerSelection[] getMyTeamSelections() {
		return myTeamSelections;
	}

	/**
	 * @return the ennemyChampions
	 */
	public PlayerSelection[] getEnnemySelections() {
		return ennemySelections;
	}

	/**
	 * @param myTeamChampions the myTeamChampions to set
	 */
	public void setMyTeamSelections(PlayerSelection[] myTeamChampions) {
		this.myTeamSelections = myTeamChampions;
	}
	
	/**
	 * @return the lanesList
	 */
	public List<Lane> getLanesList() {
		return lanesList;
	}
	
	
	/**
	 * @return the lanesMap
	 */
	public HashMap<String, Lane> getLanesMap() {
		return lanesMap;
	}

	/**
	 * @param lanesMap the lanesMap to set
	 */
	public void setLanesMap(HashMap<String, Lane> lanesMap) {
		this.lanesMap = lanesMap;
	}

	public String toString()
	{
		final StringBuffer buff = new StringBuffer();
		
		buff.append("\n-----------------------\n");
		buff.append("OPTIONS"+"\n");
		buff.append("- Meta : "+this.isWithMeta+"\n");
		if(lane!=null)
		{
			buff.append("- Lane : "+this.lane.getLaneName()+"\n");
		}
		else
		{
			buff.append("- Lane : "+this.lane+"\n");
		}
		
		buff.append("\nMY TEAM SELECTION\n");
		
		for(int i=0;i< Constant.NUMBER_PLAYER_PER_TEAM;i++)
		{
	    	if(this.myTeamSelections[i]!=null)
	    	{	    				
	    		buff.append("- player "+(i+1)+" : ");
				if(this.myTeamSelections[i].getChampion()!=null)
				{
					buff.append(this.myTeamSelections[i].getChampion().getName());					
				}
	    		buff.append(" - ");
	    		if(this.myTeamSelections[i].getLane()!=null)
				{
	    			buff.append(this.myTeamSelections[i].getLane().getLaneName());	
				}
	    		buff.append("\n");
	    	}
		}
		
		buff.append("\nENNEMY TEAM SELECTION\n");
		for(int i=0;i< this.ennemySelections.length;i++)
		{
			if(this.ennemySelections[i]!=null)
			{
				buff.append("- enmemy "+(i+1)+" : ");
				if(this.ennemySelections[i].getChampion()!=null)
				{
					buff.append(this.ennemySelections[i].getChampion().getName());					
				}
	    		buff.append(" - ");
	    		if(this.ennemySelections[i].getLane()!=null)
				{
	    			buff.append(this.ennemySelections[i].getLane().getLaneName());	
				}
	    		buff.append("\n");
			}
		}
	    
		buff.append("\nBANNED CHAMPIONS\n");
		for(int i=0;i< this.bannedChampions.length;i++)
		{
			if(this.bannedChampions[i]!=null)
			{
				buff.append("- "+this.bannedChampions[i].getName()+"\n");
			}
		}
		buff.append("-----------------------\n");
		
		return buff.toString();
	}


	/**
	 * @return the lane
	 */
	public Lane getLane() {
		return lane;
	}


	/**
	 * @param lane the lane to set
	 */
	public void setLane(Lane lane) {
		this.lane = lane;
	}

	/**
	 * @return the logger
	 */
	public static Logger getLogger() {
		return logger;
	}
}
